package org.jeecg.modules.sggg.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.sggg.entity.SgHealthClass;
import org.jeecg.modules.sggg.service.ISgHealthClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
 * @Description: 健康课堂表
 * @Author: jeecg-boot
 * @Date: 2022-10-17
 * @Version: V1.0
 */
@Api(tags = "健康课堂表")
@RestController
@RequestMapping("/sggg/sgHealthClass")
@Slf4j
public class SgHealthClassController extends JeecgController<SgHealthClass, ISgHealthClassService> {
    @Autowired
    private ISgHealthClassService sgHealthClassService;

    @ApiOperation(value = "健康课堂表-分页列表查询", notes = "健康课堂表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<SgHealthClass>> queryPageList(SgHealthClass sgHealthClass,
                                                      @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                      HttpServletRequest req) {
        QueryWrapper<SgHealthClass> queryWrapper = QueryGenerator.initQueryWrapper(sgHealthClass, req.getParameterMap());
        Page<SgHealthClass> page = new Page<>(pageNo, pageSize);
        IPage<SgHealthClass> pageList = sgHealthClassService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    @ApiOperation(value = "健康课堂表-用于小程序", notes = "健康课堂表-分页列表查询")
    @GetMapping(value = "/queryList")
    public Result<IPage<SgHealthClass>> queryList(SgHealthClass hc,
                                                  @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        QueryWrapper<SgHealthClass> queryWrapper = new QueryWrapper<>();
        if (Strings.isNotBlank(hc.getTitle())) {
            queryWrapper.like("title", hc.getTitle());
        }
        if (hc.getType() != null) {
            queryWrapper.eq("type", hc.getType());
        }
        Page<SgHealthClass> page = new Page<>(pageNo, pageSize);
        IPage<SgHealthClass> pageList = sgHealthClassService.page(page, queryWrapper.orderByDesc("create_time"));
        return Result.OK(pageList);
    }

    @AutoLog(value = "健康课堂表-添加")
    @ApiOperation(value = "健康课堂表-添加", notes = "健康课堂表-添加")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody SgHealthClass sgHealthClass) {
        //设置初始阅读量为0
        sgHealthClass.setReading(0);
        sgHealthClassService.save(sgHealthClass);
        return Result.OK("添加成功！");
    }

    @AutoLog(value = "健康课堂表-编辑")
    @ApiOperation(value = "健康课堂表-编辑", notes = "健康课堂表-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody SgHealthClass sgHealthClass) {
        sgHealthClassService.updateById(sgHealthClass);
        return Result.OK("编辑成功!");
    }

    @AutoLog(value = "健康课堂表-通过id删除")
    @ApiOperation(value = "健康课堂表-通过id删除", notes = "健康课堂表-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id") String id) {
        sgHealthClassService.removeById(id);
        return Result.OK("删除成功!");
    }

    @AutoLog(value = "健康课堂表-批量删除")
    @ApiOperation(value = "健康课堂表-批量删除", notes = "健康课堂表-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids") String ids) {
        this.sgHealthClassService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }


    @ApiOperation(value = "健康课堂表-通过id查询", notes = "健康课堂表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<SgHealthClass> queryById(@RequestParam(name = "id") String id) {
        SgHealthClass sgHealthClass = sgHealthClassService.getById(id);
        if (sgHealthClass == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(sgHealthClass);
    }

    /**
     * 导出excel
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SgHealthClass sgHealthClass) {
        return super.exportXls(request, sgHealthClass, SgHealthClass.class, "健康课堂表");
    }

    /**
     * 通过excel导入数据
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, SgHealthClass.class);
    }

    /**
     * 小程序-健康课堂-增加阅读量
     */
    @ApiOperation(value = "健康课堂表-增加阅读量", notes = "健康课堂表-增加阅读量")
    @PostMapping(value = "/editeditReading")
    public void editReading(@RequestBody SgHealthClass sgHealthClass) {
        SgHealthClass sgHealth = sgHealthClassService.getById(sgHealthClass.getId());
        sgHealth.setReading(sgHealth.getReading() + 1);
        sgHealthClassService.updateById(sgHealth);
    }

}
